#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Last Update:

import sys
sys.path.append('.')
sys.path.append('..')
import wx

class MainPanel(wx.Panel):
    def __init__(self, parent, log, **kargs):
        wx.Panel.__init__(self, parent)
        self.log = log
        self.lblAction = wx.StaticText(self, -1, u'行动力限制:')
        self.lblKind = wx.StaticText(self, -1, u'作物限制:')
        self.lblCount = wx.StaticText(self, -1, u'采摘量限制(*):')

        self.lblAction1 = wx.StaticText(self, -1, u'只使用')
        self.txtAction = wx.TextCtrl(self, -1)
        self.lblAction2 = wx.StaticText(self, -1, u'行动力')

        self.lblKind1 = wx.StaticText(self, -1, u'可以采摘')
        self.ckbFood = wx.CheckBox(self, -1, u'粮食')
        self.ckbWood = wx.CheckBox(self, -1, u'木材')
        self.ckbStone = wx.CheckBox(self, -1, u'石料')
        self.ckbBronze = wx.CheckBox(self, -1, u'青铜')

        self.group_radio = []
        self.rabCountLv = wx.RadioButton(self, -1, u'采摘lv')
        self.txtCountLv = wx.TextCtrl(self, -1)
        self.lblCountLv= wx.StaticText(self, -1, u'以上作物')
        self.group_radio.append((self.rabCountLv, self.txtCountLv, self.lblCountLv))

        self.rabCountSum = wx.RadioButton(self, -1, u'采摘产量')
        self.txtCountSum = wx.TextCtrl(self, -1)
        self.lblCountSum= wx.StaticText(self, -1, u'以上作物')
        self.group_radio.append((self.rabCountSum, self.txtCountSum, self.lblCountSum))

        for rab, txt, lbl in self.group_radio:
            self.Bind(wx.EVT_RADIOBUTTON, self.OnCountSelect, rab)

        self.layout()
        self.init()
        
    def layout(self):
        self.Center()

        actionSizer = wx.BoxSizer(wx.HORIZONTAL)
        actionSizer.Add(self.lblAction1, 0, wx.ALL, 5)
        actionSizer.Add(self.txtAction, 0, wx.ALL, 5)
        actionSizer.Add(self.lblAction2, 0, wx.ALL, 5)

        kindSizer = wx.BoxSizer(wx.HORIZONTAL)
        kindSizer.Add(self.lblKind1, 0, wx.ALL, 5)
        kindSizer.Add(self.ckbFood, 0, wx.ALL, 5)
        kindSizer.Add(self.ckbWood, 0, wx.ALL, 5)
        kindSizer.Add(self.ckbStone, 0, wx.ALL, 5)
        kindSizer.Add(self.ckbBronze, 0, wx.ALL, 5)

        radioSizer = wx.FlexGridSizer(cols=3, hgap=5, vgap=5)
        for rab, txt, lbl in self.group_radio:
            radioSizer.Add(rab, 0, wx.EXPAND)
            radioSizer.Add(txt, 0, wx.EXPAND)
            radioSizer.Add(lbl, 0, wx.EXPAND)

        userSizer = wx.FlexGridSizer(cols=2, hgap=5, vgap=5)
        userSizer.AddGrowableCol(1)
        userSizer.Add(self.lblAction, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
        userSizer.Add(actionSizer, 0, wx.ALL, 5)
        userSizer.Add(self.lblKind, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
        userSizer.Add(kindSizer, 0, wx.EXPAND)
        userSizer.Add(self.lblCount, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
        userSizer.Add(radioSizer, 0, wx.EXPAND)

        setupBox = wx.StaticBox(self, -1, u'农场设置')
        setupSizer = wx.StaticBoxSizer(setupBox, wx.VERTICAL)
        setupSizer.Add(userSizer, 0, wx.EXPAND|wx.ALL, 5)

        mainSizer = wx.BoxSizer(wx.VERTICAL)
        mainSizer.Add(setupSizer, 0, wx.EXPAND|wx.ALL, 5)

        self.SetSizer(mainSizer)

    def init(self):
        self.txtAction.SetValue('0')
        self.rabCountLv.SetValue(1)
        self.txtCountLv.SetValue('4')
        self.txtCountSum.Enable(False)

    def OnCountSelect(self, evt):
        radio_selected = evt.GetEventObject()
        for rab, txt, lbl in self.group_radio:
            if rab is radio_selected:
                txt.Enable(True)
            else:
                txt.SetValue('')
                txt.Enable(False)

    def getData(self, data={}):
        def getFarmKind():
            farmkind = {}
            farmkind.update({'food':self.ckbFood.GetValue()})
            farmkind.update({'wood':self.ckbWood.GetValue()})
            farmkind.update({'stone':self.ckbStone.GetValue()})
            farmkind.update({'bronze':self.ckbBronze.GetValue()})
            return farmkind

        def getFarmCount():
            if self.rabCountLv.GetValue():
                return {'kind':'lv', 'value':int(self.txtCountLv.GetValue())}
            if self.rabCountSum.GetValue():
                return {'kind':'sum', 'value':int(self.txtCountSum.GetValue())}
            return None

        data.update({'farmKind':getFarmKind()})
        data.update({'farmAction':int(self.txtAction.GetValue())})
        data.update({'farmCount':getFarmCount()})
        return data

    def setData(self, data):
        farmKind = data['farmKind']
        self.ckbFood.SetValue(farmKind['food'])
        self.ckbWood.SetValue(farmKind['wood'])
        self.ckbStone.SetValue(farmKind['stone'])
        self.ckbBronze.SetValue(farmKind['bronze'])

        self.txtAction.SetValue(str(data['farmAction']))

        farmCount = data['farmCount']
        if farmCount['kind'] == 'lv':
            self.rabCountLv.SetValue(True)
            self.txtCountLv.SetValue(str(farmCount['value']))
            self.txtCountSum.SetValue('')
        elif farmCount['kind'] == 'sum':
            self.rabCountSum.SetValue(True)
            self.txtCountSum.SetValue(str(farmCount['value']))
            self.txtCountLv.SetValue('')

if __name__ == '__main__':
    import config
    app = wx.PySimpleApp()
    mainFrame = wx.Frame(None, -1, 'main', size=(800, 600))
    MainPanel(mainFrame, config.log)
    mainFrame.Show()
    app.MainLoop()    

